package com.interceptor;

import com.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author chenhailong
 * @ClassName: TextAop
 * @Description: 自定义切面
 * @date 2021/12/24 19:39
 * @Version 1.0
 */
@Aspect
@Component
@Slf4j
public class TestAop {


    //    @Pointcut("execution(* com.controller.*.*(..))")   扫描全部controller
    @Pointcut("execution(* com.controller.ITTreeNodeController.*(..))")
    public void aspectForPub() {
        log.info("CacheAspect--》aspect");
    }

    @Around("aspectForPub()")
    public Object logRecored(ProceedingJoinPoint joinPoint) throws Throwable {
        //swagger上的注解
        try {
            Long start = System.currentTimeMillis();
            Api a = (Api) (joinPoint.getSignature().getDeclaringType().getAnnotation(Api.class));
            log.info("**************测试切点************************Api类名==={}", a.value());
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();

            ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
            log.info("**************测试切点************************apiOperation方法名==={}", apiOperation.value());
            Object[] Args = joinPoint.getArgs();
            Object proceed = joinPoint.proceed(Args);
            log.info("**************耗时************************spendTime==={}ms", System.currentTimeMillis() - start);

            return proceed;
        } catch (Exception e) {
            log.error("更新redis数据异常 errorinfo={}", e);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return Response.createError("系统异常，请联系管理员");


    }


}
